package com.kele.job;

import com.kele.ddl.CreateTable;
import com.kele.entity.DbModel;

import java.sql.*;
import java.util.*;

public class FindDb {

    static Scanner sc;
    static String tableName;
    static ResultSet resultSet;
    static Statement stmt;
    static Connection conn;
    static  ResultSetMetaData metaData;



    public static void main(String[] args) throws SQLException {

        FindDb findDb=new FindDb();
        List list = findDb.execute();
        System.out.println(list);

    }

    public  List execute() throws SQLException {

        System.out.println("输入表名");
        sc=new Scanner(System.in);
        tableName=sc.next();

        conn= CreateTable.getConnection();

        DbModel dbModel = CreateTable.getDbDetails(tableName);

        String sql="select * from "+tableName;
        stmt = conn.createStatement();

        resultSet=stmt.executeQuery(sql);

        metaData=resultSet.getMetaData();
        int columnCount=metaData.getColumnCount();

        List<String> list = new ArrayList<>();
        Map<String,String> map=new HashMap<>(16);
        while (resultSet.next()){
            for (int i = 1; i <=columnCount ; i++) {
                map.put(metaData.getColumnName(i), resultSet.getString(i));

                //list.add(map);
            }
            //System.out.println(map);
            //System.out.println(list);
//            for(Object s:map.keySet()){
//                System.out.println("key : "+s+" value : "+map.get(s));
//               list.add(map);
//            }
            for (Map.Entry<String, String> entry:map.entrySet()){
                list.add(entry.getKey()+" : "+entry.getValue());
            }
        }
        return list;

    }


}
